<?php
class Mysql{
	private $db = null;
	private $ob = null;
	private $debug = 0;										//打开sql debug
	private $debug_stop = 0;								//是否中止sql

	function __construct($dbhost,$dbuser,$dbpass,$dbname,$charset='UTF8',$pconnect='0'){
		if($pconnect)
		$this->db = mysql_pconnect($dbhost, $dbuser, $dbpass);
		else
		$this->db = mysql_connect($dbhost, $dbuser, $dbpass);
		if(!$this->db){
			die("Can not connect the database!");
		}

		if(!mysql_select_db($dbname,$this->db)){
			die('Cannot use the database ');
		}
		mysql_query("SET NAMES ".$charset,$this->db);
	}
	function __destruct(){
		//unset($this->db);
	}
	
	//取一条记录,一维
	function getRow($sql){
		$this->checkDebug($sql);
		$this->ob =mysql_query($sql,$this->db);
		if($this->ob)
		$rs = mysql_fetch_array ($this->ob,MYSQL_ASSOC);
		return $rs;
	}
	
	//取所有记录,二维
	function getAll($sql){
		$this->checkDebug($sql);
		$this->ob =mysql_query($sql,$this->db);
		if($this->ob)
		while ($row = mysql_fetch_array ($this->ob,MYSQL_ASSOC)){
				$rs[]=$row;
		};
		return $rs;
	}
	
	//取一个字段值
	function getOne($sql){
		$this->checkDebug($sql);
		$this->ob =mysql_query($sql,$this->db);
		if($this->ob)
		$rs = mysql_fetch_row ($this->ob);
		return $rs[0];
	}
	
	//添加记录返回最后的插入自增主键,oracle无效
	function getInsertId(){
		return mysql_insert_id($this->db);
	}
	
	//操作执行insert update delete,返回影响条数
	function execute($sql){
		$rs = mysql_query($sql,$this->db);
		$rs = mysql_affected_rows($this->db);
		return $rs>0 ? $rs : false;
	}
	
	function insert($table,$data){
		if(!is_array($data) || !$table){
			die("参数有误");
		}
		$field =  '';
		$value =  '';
		foreach($data as $k=>$v){
			$field .= '`'.$k.'`,';
			$value .= '"'.$this->inputSQL($v).'",';	
		}
		$field = rtrim($field,',');
		$value = rtrim($value,',');
		$sql = "INSERT INTO `".$table."` (".$field.") VALUES(".$value.")";
		//die($sql);
		$this->checkDebug($sql);
		$rs = $this->execute($sql);
		return $rs;
	}
	
	function update($table,$data,$where){
		if(!is_array($data) || !$table || !$where){
			die("参数有误");
		}
		$list = '';
		foreach($data as $k=>$v){
			if(strstr($v,$k)){
				$list .= '`'.$k.'` = '.$v.',';						//处理增量方式
			}else{
				$list .= '`'.$k.'` = "'.$this->inputSQL($v).'" ,';
			}
		}
		$list = rtrim($list,',');
		$sql = "UPDATE `".$table."` SET ".$list." WHERE ".$where;
		//die($sql);
		$this->checkDebug($sql);
		$rs = $this->execute($sql);
		return $rs; 
	}
	
	function delete($table,$where){
		if(!$table){
			die("参数有误");
		}
		$sql = "DELETE FROM `".$table."` WHERE ".$where ;
		$this->checkDebug($sql);
		$rs = $this->execute($sql);
		return $rs; 
	}
	
	private function getField($table){
	    $sql = "SELECT COLUMN_NAME FROM `information_schema`.`COLUMNS` WHERE `TABLE_SCHEMA`='".$this->dbname."' AND `TABLE_NAME`='".$table."'";   
	    return $this->getAll($sql);
	}
	
	//数据库表与表单值映射,获取更新数据值
	function getFormData($table,$post){
	    $fields = $this->getField($table);
	    $data = array();
	    if($fields){
		foreach ($fields as $v) {
		    $filed = $v['COLUMN_NAME'];
		    if(isset($post[$filed])){
			$data[$filed] = $post[$filed];
		    }
		}
	    }
	    return $data;
	}
	
	//检查输出sql
	function checkDebug($sql){
		if ($this->debug) {
			echo $sql.'<br />';
		}
		if($this->debug_stop){
			exit;
		}
	}
	//设定是否debug SQL语句
	function debug($type=1,$stop=0){
		$this->debug = intval($type);
		if(intval($stop)){
			$this->debug_stop = 1;
		}
	}
	
	function inputSQL($sql){
		if(!get_magic_quotes_gpc()){
			return addslashes($sql);
		}else{
			return $sql;
		}
	}
	
	function outputSQL($sql){
		if(!get_magic_quotes_gpc()){
			return stripslashes($sql);
		}else{
			return $sql;
		}
	}
	//事务处理开始
	function begin(){
		//mysql_query("set autocommit='0';",$this->db);
		mysql_query("begin",$this->db);
	}
	//事务返回
	function back(){
		mysql_query("rollback",$this->db);
		//mysql_query("SET AUTOCOMMIT='1'",$this->db);
	}
	//事务执行
	function commit(){
		mysql_query("commit",$this->db);
		//mysql_query("SET AUTOCOMMIT='1'",$this->db);

	}
	
} 


//example
/*
$newdb = new Mysql("localhost","root","111111","my_show");
$indata = array("news_title"=>time(),"news_sort_title"=>time(),"news_content"=>'xxxx','news_create_info'=>'aaa','news_ip'=>'192.168.1.1');
//$newdb->setDebug(1,1);
$indata2 = array("user_name"=>time(),"user_pwd"=>'1111',"user_ip"=>'192.168.1.1');
$indata3 = array("user_name"=>time(),"user_pwd"=>'2222',"user_ip"=>'192.168.1.1');
$newdb->begin();
$rs = $newdb->insert("icc_user",$indata2);
$rs3 = $newdb->insert("icc_user",$indata3);
if($rs>0 && $rs3>0){
	$newdb->commit();
}else{
	$newdb->back();
}
$rs = $newdb->insert("icc_user",$indata2);
$rs = $newdb->update("icc_news",$indata,"news_id='203'");
$rs = $newdb->delete("icc_news","news_id='203'");
*/
?>